﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.BLL;
using Millionaire;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class FrmMain : Form
    {
        CauHoiBLL cauHoiBLL;
        public FrmMain()
        {
            InitializeComponent();
            cauHoiBLL = new CauHoiBLL();
        }

      

        private void lứaTuổiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmLuaTuoi frm = new FrmLuaTuoi();
            frm.MdiParent = this;
            frm.Show();
        }

        private void lĩnhVựcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmLinhVuc frm = new FrmLinhVuc();
            frm.MdiParent = this;
            frm.Show();
        }

        private void thêmCâuHỏiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmCauHoi frm = new FrmCauHoi();
            frm.MdiParent = this;
            frm.Show();
        }

        private void fromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            DialogResult ds = fileDialog.ShowDialog();
            if (ds == System.Windows.Forms.DialogResult.OK)
            {
                string path = fileDialog.FileName;
            LoadFromFile(path);
            }
        }

        private void LoadFromFile(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            while (!sr.EndOfStream)
            {
                string ch = sr.ReadLine();
                InsertCauHoi(ch);
            }
            MessageBox.Show("success");
        }

        private void InsertCauHoi(string ch)
        {
            string[] strCH = new string[6];
            strCH = ch.Split(',');
            CauHoi cauHoi = new CauHoi();
            cauHoi.CauHoi1 = strCH[0].Replace('|', ',');
            cauHoi.CauA = strCH[1].Replace('|', ',');
            cauHoi.CauB = strCH[2].Replace('|', ',');
            cauHoi.CauC = strCH[3].Replace('|', ',');
            cauHoi.CauD = strCH[4].Replace('|', ',');
            cauHoi.DapAn = strCH[5].Trim();
            cauHoi.MaLinhVuc = 1;
            cauHoi.MucDo = 0;
            cauHoi.MaLuaTuoi = 1;

            cauHoiBLL.AddNew(cauHoi);
        }

        private void chơiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmCheDoChoi frm = new FrmCheDoChoi();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void danhSáchCâuHỏiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmEditCauHoi frm = new FrmEditCauHoi();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
    }
}
